package pers.hyj.ims.view;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author H_Yunjia
 */

public class UserView extends JFrame {

    public UserView(){
        super();
        setTitle("管理员界面");
        setLocation(450,300);
        setSize(800,500);
        setResizable(false);
        initUser();
    }

    //组件
    //<editor-fold> defaultstate="collapsed"
    //选项卡
    private JTabbedPane tabbedPane = new JTabbedPane();

    //界面主容器
    private JPanel jPanel_Main_1 = new JPanel();
    private JPanel jPanel_Main_3 = new JPanel();
    private JPanel jPanel_Main_4 = new JPanel();

    //<editor-fold> desc="Main_1_信息界面" defaultstate="collapsed"

    //用户树_Main_1
    private JScrollPane M1_jScrollPane = new JScrollPane();
    private JTree M1_tree=new JTree();


    //用户信息显示_Main_1

    //一级容器_Main_1
    private JPanel M1_jPanel_lift=new JPanel();
    private JPanel M1_jPanel_right=new JPanel();

    //二级容器_Main_1
    private JPanel M1_jp1=new JPanel();
    private JPanel M1_jp2=new JPanel();
    private JPanel M1_jp3=new JPanel();
    private JPanel M1_jp4=new JPanel();
    private JPanel M1_jp5=new JPanel();
    private JPanel M1_jp6=new JPanel();
    private JPanel M1_jp7=new JPanel();
    //</editor-fold>

    //<editor-fold> desc="Main_3_修改界面" defaultstate="collapsed"

    //用户树_Main_3
    private JScrollPane M3_jScrollPane = new JScrollPane();
    private JTree M3_tree=new JTree();

    //一级容器_Main_3
    private JPanel M3_jPanel_lift=new JPanel();
    private JPanel M3_jPanel_right=new JPanel();

    //二级容器_Main_3
    private JPanel M3_jp1=new JPanel();
    private JPanel M3_jp2=new JPanel();
    private JPanel M3_jp3=new JPanel();
    private JPanel M3_jp4=new JPanel();
    private JPanel M3_jp5=new JPanel();
    private JPanel M3_jp6=new JPanel();
    private JPanel M3_jp7=new JPanel();
    private JPanel M3_jp8=new JPanel();
    private JPanel M3_jp9=new JPanel();
    private JPanel M3_jp10=new JPanel();
    private JPanel M3_jp11=new JPanel();

    //</editor-fold>

    //<editor-fold> desc="Main_4_统计界面" defaultstate="collapsed"

    //一级容器_Main_4
    private JPanel M4_jPanel_lift=new JPanel();
    private JPanel M4_jPanel_right=new JPanel();

    //二级容器_Main_4
    private JPanel M4_jp1=new JPanel();
    private JPanel M4_jp2=new JPanel();
    private JPanel M4_jp3=new JPanel();
    private JPanel M4_jp4=new JPanel();
    private JPanel M4_jp5=new JPanel();
    private JPanel M4_jp6=new JPanel();
    private JPanel M4_jp7=new JPanel();
    private JPanel M4_jp8=new JPanel();
    private JPanel M4_jp9=new JPanel();
    private JPanel M4_jp10=new JPanel();

    //</editor-fold>

    //</editor-fold>

    public void initUser(){

        //设置默认字体
        Font font = new Font("华文中宋",Font.BOLD,15);
        UIManager.put("Label.font",font);
        UIManager.put("TextField.font",font);

        //<editor-fold> desc="主界面" defaultstate="collapsed"
        //选项卡
        add(tabbedPane);

        //界面主容器
        jPanel_Main_1.setLayout(new GridLayout(1,3));
        tabbedPane.addTab("信息列表",null,jPanel_Main_1);
        jPanel_Main_3.setLayout(new GridLayout(1,3));
        tabbedPane.addTab("修改信息",null,jPanel_Main_3);
        jPanel_Main_4.setLayout(new GridLayout(1,2));
        tabbedPane.addTab("统计信息",null,jPanel_Main_4);
        //</editor-fold>

        //<editor-fold> desc="Main_1_用户列表" defaultstate="collapsed"

        //Main_1_左树
        //<editor-fold> defaultstate="collapsed"
        //滚条
        jPanel_Main_1.add(M1_jScrollPane);

        //树
        treeDisplay_1();
        M1_jScrollPane.setViewportView(M1_tree);

        //</editor-fold>

        //Main_1_右信息
        //<editor-fold> defaultstate="collapsed"

        //<editor-fold> desc="容器" defaultstate="collapsed"
        //信息一级容器
        M1_jPanel_lift.setLayout(new GridLayout(7,1));
        M1_jPanel_right.setLayout(new GridLayout(7,1));
        jPanel_Main_1.add(M1_jPanel_lift);
        jPanel_Main_1.add(M1_jPanel_right);

        //信息二级容器
        M1_jp1.setLayout(new GridLayout(1,2));
        M1_jPanel_lift.add(new JLabel(""));
        M1_jPanel_lift.add(M1_jp1);

        M1_jp2.setLayout(new GridLayout(1,2));
        M1_jPanel_lift.add(M1_jp2);

        M1_jp3.setLayout(new GridLayout(1,2));
        M1_jPanel_lift.add(M1_jp3);

        M1_jp4.setLayout(new GridLayout(1,2));
        M1_jPanel_lift.add(M1_jp4);

        M1_jp5.setLayout(new GridLayout(1,2));
        M1_jPanel_lift.add(M1_jp5);
        M1_jPanel_lift.add(new JLabel(""));

        M1_jp6.setLayout(new GridLayout(1,2));
        M1_jPanel_right.add(new JLabel());
        M1_jPanel_right.add(M1_jp6);
        M1_jPanel_right.add(new JPanel());

        M1_jp7.setLayout(new GridLayout(1,2));
        M1_jPanel_right.add(M1_jp7);
        M1_jPanel_right.add(new JPanel());
        M1_jPanel_right.add(new JPanel());
        M1_jPanel_right.add(new JLabel());

        //</editor-fold>

        //二级容器内标签
        JLabel M1_jp1_1=new JLabel("姓名",JLabel.CENTER);
        JLabel M1_jp1_2=new JLabel("");
        M1_jp1.add(M1_jp1_1);
        M1_jp1.add(M1_jp1_2);

        JLabel M1_jp2_1=new JLabel("学号",JLabel.CENTER);
        JTextField M1_jp2_2=new JTextField("");
        M1_jp2.add(M1_jp2_1);
        M1_jp2.add(M1_jp2_2);

        JLabel M1_jp3_1=new JLabel("身份",JLabel.CENTER);
        JLabel M1_jp3_2=new JLabel("");
        M1_jp3.add(M1_jp3_1);
        M1_jp3.add(M1_jp3_2);

        JLabel M1_jp4_1=new JLabel("所属院系",JLabel.CENTER);
        JLabel M1_jp4_2=new JLabel("");
        M1_jp4.add(M1_jp4_1);
        M1_jp4.add(M1_jp4_2);

        JLabel M1_jp5_1=new JLabel("所属单位",JLabel.CENTER);
        JLabel M1_jp5_2=new JLabel("");
        M1_jp5.add(M1_jp5_1);
        M1_jp5.add(M1_jp5_2);

        JLabel M1_jp6_1=new JLabel("性别",JLabel.CENTER);
        JLabel M1_jp6_2=new JLabel("");
        M1_jp6.add(M1_jp6_1);
        M1_jp6.add(M1_jp6_2);

        JLabel M1_jp7_1=new JLabel("");
        JButton M1_jp7_2=new JButton("查  询");
        M1_jp7_2.setFont(new Font("华文中宋",Font.BOLD,20));
        M1_jp7.add(M1_jp7_1);
        M1_jp7.add(M1_jp7_2);

        //</editor-fold>

        //</editor-fold>

        //<editor-fold> desc="Main_3_修改列表" defaultstate="collapsed"

        //Main_3_左树
        //<editor-fold> defaultstate="collapsed"
        //滚条
        jPanel_Main_3.add(M3_jScrollPane);

        //树
        treeDisplay_3();
        M3_jScrollPane.setViewportView(M3_tree);
        //</editor-fold>

        //<editor-fold> desc="容器" defaultstate="collapsed"
        //信息一级容器
        M3_jPanel_lift.setLayout(new GridLayout(7,1));
        M3_jPanel_right.setLayout(new GridLayout(7,1));
        jPanel_Main_3.add(M3_jPanel_lift);
        jPanel_Main_3.add(M3_jPanel_right);

        //信息二级容器
        M3_jp1.setLayout(new GridLayout(1,2));
        M3_jPanel_lift.add(new JLabel(""));
        M3_jPanel_lift.add(M3_jp1);

        M3_jp2.setLayout(new GridLayout(1,2));
        M3_jPanel_lift.add(M3_jp2);

        M3_jp3.setLayout(new GridLayout(1,2));
        M3_jPanel_lift.add(M3_jp3);

        M3_jp4.setLayout(new GridLayout(1,2));
        M3_jPanel_lift.add(M3_jp4);

        M3_jp5.setLayout(new GridLayout(1,2));
        M3_jPanel_lift.add(M3_jp5);
        M3_jPanel_lift.add(new JLabel(""));

        M3_jp6.setLayout(new GridLayout(1,2));
        M3_jPanel_right.add(new JLabel());
        M3_jPanel_right.add(M3_jp6);

        M3_jp7.setLayout(new GridLayout(1,2));
        M3_jPanel_right.add(M3_jp7);

        M3_jp8.setLayout(new GridLayout(1,2));
        M3_jPanel_right.add(M3_jp8);

        M3_jp9.setLayout(new GridLayout(1,2));
        M3_jPanel_right.add(M3_jp9);

        M3_jp10.setLayout(new GridLayout(1,2));
        M3_jPanel_right.add(M3_jp10);

        M3_jp11.setLayout(new GridLayout(1,2));
        M3_jPanel_right.add(M3_jp11);
        //</editor-fold>

        //二级容器内标签
        JLabel M3_jp1_1=new JLabel("姓名",JLabel.CENTER);
        JTextField M3_jp1_2=new JTextField("");
        M3_jp1.add(M3_jp1_1);
        M3_jp1.add(M3_jp1_2);

        JLabel M3_jp2_1=new JLabel("学号",JLabel.CENTER);
        JTextField M3_jp2_2=new JTextField("");
        M3_jp2.add(M3_jp2_1);
        M3_jp2.add(M3_jp2_2);

        JLabel M3_jp3_1=new JLabel("身份",JLabel.CENTER);
        JTextField M3_jp3_2=new JTextField("");
        M3_jp3.add(M3_jp3_1);
        M3_jp3.add(M3_jp3_2);

        JLabel M3_jp4_1=new JLabel("所属院系",JLabel.CENTER);
        JTextField M3_jp4_2=new JTextField("");
        M3_jp4.add(M3_jp4_1);
        M3_jp4.add(M3_jp4_2);

        JLabel M3_jp5_1=new JLabel("所属单位",JLabel.CENTER);
        JTextField M3_jp5_2=new JTextField("");
        M3_jp5.add(M3_jp5_1);
        M3_jp5.add(M3_jp5_2);

        JLabel M3_jp6_1=new JLabel("性别",JLabel.CENTER);
        JTextField M3_jp6_2=new JTextField("");
        M3_jp6.add(M3_jp6_1);
        M3_jp6.add(M3_jp6_2);

        JLabel M3_jp7_1=new JLabel("密码",JLabel.CENTER);
        JTextField M3_jp7_2=new JTextField("");
        M3_jp7.add(M3_jp7_1);
        M3_jp7.add(M3_jp7_2);

        JLabel M3_jp8_1=new JLabel("");
        JButton M3_jp8_2=new JButton("查  询");
        M3_jp8_2.setFont(new Font("华文中宋",Font.BOLD,20));
        M3_jp8.add(M3_jp8_1);
        M3_jp8.add(M3_jp8_2);

        JLabel M3_jp9_1=new JLabel("");
        JButton M3_jp9_2=new JButton("修  改");
        M3_jp9_2.setFont(new Font("华文中宋",Font.BOLD,20));
        M3_jp9.add(M3_jp9_1);
        M3_jp9.add(M3_jp9_2);

        JLabel M3_jp10_1=new JLabel("");
        JButton M3_jp10_2=new JButton("添  加");
        M3_jp10_2.setFont(new Font("华文中宋",Font.BOLD,20));
        M3_jp10.add(M3_jp10_1);
        M3_jp10.add(M3_jp10_2);

        JLabel M3_jp11_1=new JLabel("");
        JButton M3_jp11_2=new JButton("删  除");
        M3_jp11_2.setFont(new Font("华文中宋",Font.BOLD,20));
        M3_jp11.add(M3_jp11_1);
        M3_jp11.add(M3_jp11_2);

        //</editor-fold>

        //<editor-fold> desc="Main_4_统计界面" defaultstate="collapsed"

        //<editor-fold> desc="容器" defaultstate="collapsed"
        //信息一级容器
        M4_jPanel_lift.setLayout(new GridLayout(7,1));
        M4_jPanel_right.setLayout(new GridLayout(7,1));
        jPanel_Main_4.add(M4_jPanel_lift);
        jPanel_Main_4.add(M4_jPanel_right);

        //信息二级容器
        M4_jp1.setLayout(new GridLayout(1,2));
        M4_jPanel_lift.add(new JLabel(""));
        M4_jPanel_lift.add(M4_jp1);

        M4_jp2.setLayout(new GridLayout(1,2));
        M4_jPanel_lift.add(M4_jp2);

        M4_jp3.setLayout(new GridLayout(1,2));
        M4_jPanel_lift.add(M4_jp3);

        M4_jp4.setLayout(new GridLayout(1,2));
        M4_jPanel_lift.add(M4_jp4);

        M4_jp5.setLayout(new GridLayout(1,2));
        M4_jPanel_lift.add(M4_jp5);
        M4_jPanel_lift.add(new JLabel(""));

        M4_jp6.setLayout(new GridLayout(1,2));
        M4_jPanel_right.add(new JLabel());
        M4_jPanel_right.add(M4_jp6);

        M4_jp7.setLayout(new GridLayout(1,2));
        M4_jPanel_right.add(M4_jp7);

        M4_jp8.setLayout(new GridLayout(1,2));
        M4_jPanel_right.add(M4_jp8);

        M4_jp9.setLayout(new GridLayout(1,2));
        M4_jPanel_right.add(M4_jp9);

        M4_jp10.setLayout(new GridLayout(1,2));
        M4_jPanel_right.add(M4_jp10);
        M4_jPanel_right.add(new JLabel());

        //</editor-fold>

        //二级容器内标签
        JLabel M4_jp1_1=new JLabel("统计方式",JLabel.CENTER);
        JLabel M4_jp1_2=new JLabel("请输入待查项",JLabel.CENTER);
        M4_jp1.add(M4_jp1_1);
        M4_jp1.add(M4_jp1_2);

        JLabel M4_jp2_1=new JLabel("按学院统计",JLabel.CENTER);
        JTextField M4_jp2_2=new JTextField("");
        M4_jp2.add(M4_jp2_1);
        M4_jp2.add(M4_jp2_2);

        JLabel M4_jp3_1=new JLabel("按班级统计",JLabel.CENTER);
        JTextField M4_jp3_2=new JTextField("");
        M4_jp3.add(M4_jp3_1);
        M4_jp3.add(M4_jp3_2);

        JLabel M4_jp4_1=new JLabel("按男女统计",JLabel.CENTER);
        JTextField M4_jp4_2=new JTextField("");
        M4_jp4.add(M4_jp4_1);
        M4_jp4.add(M4_jp4_2);

        JLabel M4_jp5_1=new JLabel("按身份统计",JLabel.CENTER);
        JTextField M4_jp5_2=new JTextField("");
        M4_jp5.add(M4_jp5_1);
        M4_jp5.add(M4_jp5_2);

        JLabel M4_jp6_1=new JLabel("总人数",JLabel.CENTER);
        JLabel M4_jp6_2=new JLabel("");
        M4_jp6.add(M4_jp6_1);
        M4_jp6.add(M4_jp6_2);

        JLabel M4_jp7_1=new JLabel("",JLabel.CENTER);
        JButton M4_jp7_2=new JButton("查  询");
        M4_jp7_2.setFont(new Font("华文中宋",Font.BOLD,20));
        M4_jp7.add(M4_jp7_1);
        M4_jp7.add(M4_jp7_2);

        JLabel M4_jp8_1=new JLabel("",JLabel.CENTER);
        JButton M4_jp8_2=new JButton("查  询");
        M4_jp8_2.setFont(new Font("华文中宋",Font.BOLD,20));
        M4_jp8.add(M4_jp8_1);
        M4_jp8.add(M4_jp8_2);

        JLabel M4_jp9_1=new JLabel("",JLabel.CENTER);
        JButton M4_jp9_2=new JButton("查  询");
        M4_jp9_2.setFont(new Font("华文中宋",Font.BOLD,20));
        M4_jp9.add(M4_jp9_1);
        M4_jp9.add(M4_jp9_2);

        JLabel M4_jp10_1=new JLabel("",JLabel.CENTER);
        JButton M4_jp10_2=new JButton("查  询");
        M4_jp10_2.setFont(new Font("华文中宋",Font.BOLD,20));
        M4_jp10.add(M4_jp10_1);
        M4_jp10.add(M4_jp10_2);

        //</editor-fold>

        //<editor-fold> desc="事件" defaultstate="collapsed"
//树事件_Main_1
        M1_tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                String name=String.valueOf(M1_tree.getLastSelectedPathComponent());
                Connection con=null;
                Statement stm=null;
                ResultSet rs=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    while (rs.next()) {

                        if(name.equals(rs.getString("name"))){
                            M1_jp1_2.setText(rs.getString("name"));
                            M1_jp2_2.setText(rs.getString("number"));
                            M1_jp3_2.setText(rs.getString("identity"));
                            M1_jp4_2.setText(rs.getString("college"));
                            M1_jp5_2.setText(rs.getString("unit"));
                            M1_jp6_2.setText(rs.getString("sex"));
                            break;
                        }
                    }
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//树事件_Main_3
        M3_tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                String name=String.valueOf(M3_tree.getLastSelectedPathComponent());
                Connection con=null;
                Statement stm=null;
                ResultSet rs=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    while (rs.next()) {

                        if(name.equals(rs.getString("name"))){
                            M3_jp1_2.setText(rs.getString("name"));
                            M3_jp2_2.setText(rs.getString("number"));
                            M3_jp3_2.setText(rs.getString("identity"));
                            M3_jp4_2.setText(rs.getString("college"));
                            M3_jp5_2.setText(rs.getString("unit"));
                            M3_jp6_2.setText(rs.getString("sex"));
                            M3_jp7_2.setText(rs.getString("password"));
                            break;
                        }
                    }
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//查询事件_Main_1
        M1_jp7_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con=null;
                Statement stm=null;
                ResultSet rs=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    boolean flag=true;
                    while (rs.next()) {
                        if(M1_jp2_2.getText().equals(rs.getString("number"))){
                            M1_jp1_2.setText(rs.getString("name"));
                            M1_jp6_2.setText(rs.getString("Sex"));
                            M1_jp3_2.setText(rs.getString("identity"));
                            M1_jp4_2.setText(rs.getString("college"));
                            M1_jp5_2.setText(rs.getString("unit"));
                            flag=false;
                            break;
                        }
                    }
                    if(!rs.next()&&flag)
                        JOptionPane.showMessageDialog(null,"该条信息不存在,请输入正确的学号");
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//查询事件_Main_3
        M3_jp8_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con=null;
                Statement stm=null;
                ResultSet rs=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    while (rs.next()) {
                        if(M3_jp2_2.getText().equals(rs.getString("number"))){
                            M3_jp1_2.setText(rs.getString("name"));
                            M3_jp6_2.setText(rs.getString("Sex"));
                            M3_jp3_2.setText(rs.getString("identity"));
                            M3_jp4_2.setText(rs.getString("college"));
                            M3_jp5_2.setText(rs.getString("unit"));
                            M3_jp7_2.setText(rs.getString("password"));
                            break;
                        }
                    }
                    if(!rs.next())
                        JOptionPane.showMessageDialog(null, "找不到该条信息");
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//修改事件_Main_3
        M3_jp9_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con = null;
                Statement stm = null;
                ResultSet rs = null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    boolean flag=true;
                    if(M3_jp7_2.getText().length()>18){
                        JOptionPane.showMessageDialog(null,"密码最长为18位，修改失败");
                        flag=false;
                    } else{
                        while (rs.next()) {
                            if (M3_jp2_2.getText().equals(rs.getString("number"))) {
                                stm.execute("delete from person where number=" + M3_jp2_2.getText());
                                stm.execute("insert into person(number,name,sex,identity,college,unit,password)"
                                        + "values('" + M3_jp2_2.getText() + "','" + M3_jp1_2.getText() + "','"
                                        + M3_jp6_2.getText() + "','" + M3_jp3_2.getText() + "','" + M3_jp4_2.getText()
                                        + "','" + M3_jp5_2.getText() + "','" + M3_jp7_2.getText() + "')");
                                JOptionPane.showMessageDialog(null,"修改成功");
                                treeDisplay_1();
                                treeDisplay_3();
                                flag=false;
                                break;
                            }
                        }
                        if(!rs.next()&&flag)
                            JOptionPane.showMessageDialog(null,"该学生不存在");
                    }
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//添加事件_Main_3
        M3_jp10_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con = null;
                Statement stm = null;
                ResultSet rs=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs=stm.executeQuery("select * from person");
                    if(M3_jp2_2.getText().length()!=8)
                        JOptionPane.showMessageDialog(null,"请输入8位学号，添加失败");
                    else if(M3_jp7_2.getText().length()>18)
                        JOptionPane.showMessageDialog(null,"密码最长为18位，添加失败");
                    else {
                        boolean flag=true;
                        while(rs.next())
                            if(M3_jp2_2.getText().equals(rs.getString("number")))
                                flag=false;
                        if (flag) {
                            stm.executeUpdate("insert into person(number,name,sex,identity,college,unit,password)"
                                    + "values('" + M3_jp2_2.getText() + "','" + M3_jp1_2.getText() + "','"
                                    + M3_jp6_2.getText() + "','" + M3_jp3_2.getText() + "','" + M3_jp4_2.getText()
                                    + "','" + M3_jp5_2.getText() + "','" + M3_jp7_2.getText() + "')");
                            JOptionPane.showMessageDialog(null, "添加成功");
                            treeDisplay_1();
                            treeDisplay_3();
                        }else{
                            JOptionPane.showMessageDialog(null, "该条信息已存在");
                        }
                    }
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//删除事件_Main_3
        M3_jp11_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con = null;
                Statement stm = null;
                ResultSet rs = null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    boolean flag=true;
                    while (rs.next()) {
                        if (M3_jp2_2.getText().equals(rs.getString("number"))) {
                            stm.execute("delete from person where number=" + M3_jp2_2.getText());
                            JOptionPane.showMessageDialog(null, "删除成功");
                            treeDisplay_1();
                            treeDisplay_3();
                            flag=false;
                            break;
                        }
                    }
                    if(!rs.next()&&flag)
                        JOptionPane.showMessageDialog(null, "该信息不存在");
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//查询事件_Main_4_学院
        M4_jp7_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con=null;
                Statement stm=null;
                ResultSet rs=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    int num=0;
                    while (rs.next()) {
                        if(M4_jp2_2.getText().equals(rs.getString("college"))){
                            num++;
                        }
                    }
                    String Snum=String.valueOf(num);
                    M4_jp7_1.setText(Snum);
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//查询事件_Main_4_班级
        M4_jp8_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con=null;
                Statement stm=null;
                ResultSet rs=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    int num=0;
                    while (rs.next()) {
                        if(M4_jp3_2.getText().equals(rs.getString("unit"))){
                            num++;
                        }
                    }
                    String Snum=String.valueOf(num);
                    M4_jp8_1.setText(Snum);
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//查询事件_Main_4_男女
        M4_jp9_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con=null;
                Statement stm=null;
                ResultSet rs=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    int num=0;
                    while (rs.next()) {
                        if(M4_jp4_2.getText().equals(rs.getString("sex"))){
                            num++;
                        }
                    }
                    String Snum=String.valueOf(num);
                    M4_jp9_1.setText(Snum);
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
//查询事件_Main_4_身份
        M4_jp10_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con=null;
                Statement stm=null;
                ResultSet rs=null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://localhost:3306/student?" +
                            "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                    String username = "root";
                    String password = "root";
                    con = DriverManager.getConnection(url, username, password);
                    stm = con.createStatement();
                    rs = stm.executeQuery("select * from person");
                    int num=0;
                    while (rs.next()) {
                        if(M4_jp5_2.getText().equals(rs.getString("identity"))){
                            num++;
                        }
                    }
                    String Snum=String.valueOf(num);
                    M4_jp10_1.setText(Snum);
                    rs.close();
                    stm.close();
                    con.close();
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        });
        //</editor-fold>

    }

    private void treeDisplay_1(){
        M1_tree.clearSelection();
        M1_tree.setFont(new Font("华文中宋",Font.BOLD,14));
        DefaultMutableTreeNode node=new DefaultMutableTreeNode("石家庄铁道大学人员列表");
        Connection conn=null;  //连接对象
        Statement stm=null;  //命令对象
        ResultSet rs=null;   //结果集对象
        String name=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8";
            String username = "root";
            String password = "root";
            conn= DriverManager.getConnection(url,username,password);
            stm=conn.createStatement();
            rs=stm.executeQuery("select * from person");
            while(rs.next()){
                name=rs.getString("name");
                DefaultMutableTreeNode nodeC=new DefaultMutableTreeNode(name);
                node.add(nodeC);
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        M1_tree.setModel(new DefaultTreeModel(node));
    }

    private void treeDisplay_3(){
        M3_tree.clearSelection();
        M3_tree.setFont(new Font("华文中宋",Font.BOLD,14));
        DefaultMutableTreeNode node=new DefaultMutableTreeNode("石家庄铁道大学人员列表");
        Connection conn=null;  //连接对象
        Statement stm=null;  //命令对象
        ResultSet rs=null;   //结果集对象
        String name=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8";
            String username = "root";
            String password = "root";
            conn= DriverManager.getConnection(url,username,password);
            stm=conn.createStatement();
            rs=stm.executeQuery("select * from person");
            while(rs.next()){
                name=rs.getString("name");
                DefaultMutableTreeNode nodeC=new DefaultMutableTreeNode(name);
                node.add(nodeC);
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        M3_tree.setModel(new DefaultTreeModel(node));
    }

}
